<?php
/**
 * Clavid OAUTH SuisseID example.
 * 
 * @version $Id: clavid_suisseid_callback.php 6 2010-05-17 09:19:49Z telbie $
 */


/* Start session and load library. */
session_start();
require_once('clavid_suisseid_config.php');
require_once('clavidoauth/ClavidOAuthSuisseID.php');

unset($error);

/* If the oauth_token is old redirect to the connect page. */
if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) {
  $_SESSION['oauth_status'] = 'oldtoken';
  $error = 'Found old request token in session. Refresh the page or try again later.';
} else {
  /* Create ClavidOAuthSuisseID object with app key/secret and token key/secret from default phase */
  $connection = new ClavidOAuthSuisseID(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);

  /* Request access tokens from Clavid */
  $access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']);

  unset($_SESSION['oauth_token']);
  unset($_SESSION['oauth_token_secret']);

  /* If HTTP response is 200 continue otherwise send to connect page to retry */
  if (200 == $connection->http_code) {

    if (isset($access_token[ClavidOAuthSuisseID::Clavid_Error])) {
      $error = $access_token[ClavidOAuthSuisseID::Clavid_Error];
    } else {
      /* Extract the SuisseID attributes */
      $suisseid_number = $access_token[ClavidOAuthSuisseID::SuisseID_Number];
      $suisseid_email = $access_token[ClavidOAuthSuisseID::SuisseID_EMail];
      $suisseid_name = $access_token[ClavidOAuthSuisseID::SuisseID_Name];
    }
  
  } else {
    /* Save HTTP status for error dialog on connnect page.*/
  $error = 'Could not connect to Clavid. Refresh the page or try again later.'; 
  }
	
}

?>
<html>
<head>
<title>cIAS SuisseID OAUTH PHP Login</title>
</head>
<body>

<?php
if (isset($error)) {
  echo 'ERROR: '.$error;
} else {
?>

<br>The SuisseID Number is: <?php echo $suisseid_number; ?>
<br>The SuisseID E-Mail is: <?php echo $suisseid_email; ?>
<br>The SuisseID Name is: <?php echo $suisseid_name; ?>

<?php
}
?>

<form action="index.php">
<input type="Submit" value="Back to index.php"/>
</form>
</body>
</html>
